package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CartService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private static final String MAIN = "/WEB-INF/jsp/catalog/main.jsp";
    private static final String REGISTER = "/WEB-INF/jsp/account/RegisterForm.jsp";

    private Account account;
    private Account accountTemp;
    private AccountService accountService = new AccountService();
    private CartService cartService = new CartService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        account = (Account)session.getAttribute("account");
        //将account重置
        if(account != null){
            account = null;
            session.setAttribute("account",account);
        }

        String checkCode = request.getParameter("checkCode");
        String checkCode_session =(String) session.getAttribute("checkCode_session");
        if(checkCode == null){
            session.setAttribute("messageRegister","checkCode can't be empty");
            request.getRequestDispatcher(REGISTER).forward(request,response);
        }
        else
        {
            if(checkCode.equalsIgnoreCase(checkCode_session))
            {
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String firstName = request.getParameter("firstName");
                String lastName = request.getParameter("lastName");
                String email = request.getParameter("email");
                String phone = request.getParameter("phone");
                String address1 = request.getParameter("address1");
                String address2 = request.getParameter("address2");
                String city = request.getParameter("city");
                String state = request.getParameter("state");
                String zip = request.getParameter("zip");
                String country = request.getParameter("country");
                String languagePreference = request.getParameter("languagePreference");
                String favouriteCategoryId = request.getParameter("favouriteCategoryId");
                String listOption = request.getParameter("listOption");
                String bannerOption = request.getParameter("bannerOption");

                accountTemp = new Account();
                accountTemp.setUsername(username);
                accountTemp.setPassword(password);
                accountTemp.setFirstName(firstName);
                accountTemp.setLastName(lastName);
                accountTemp.setEmail(email);
                accountTemp.setPhone(phone);
                accountTemp.setAddress1(address1);
                accountTemp.setAddress2(address2);
                accountTemp.setCity(city);
                accountTemp.setState(state);
                accountTemp.setZip(zip);
                accountTemp.setCountry(country);
                accountTemp.setLanguagePreference(languagePreference);
                accountTemp.setFavouriteCategoryId(favouriteCategoryId);
                accountTemp.setListOption(Boolean.parseBoolean(listOption));
                accountTemp.setBannerOption(Boolean.parseBoolean(bannerOption));

                accountService = new AccountService();

                accountService.insertAccount(accountTemp);
                cartService.insertNewAccount(username);
//        注册成功跳转到main界面
                session.setAttribute("account",accountTemp);
                request.getRequestDispatcher(MAIN).forward(request,response);
            }
            else
            {
                session.setAttribute("messageRegister","invalid checkCode input");
                request.getRequestDispatcher(REGISTER).forward(request,response);
            }
        }

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
